package com.platform.analysis.enums;

import com.platform.analysis.utils.FilterFunction;
import com.platform.comm.constants.EnumOption;
import com.platform.comm.enums.IEnumOption;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author liujizheng
 * @version 1.0
 * @ClassName FilterDateFunction
 * @Description 时间过滤条件
 * @date 2021/9/29 17:50
 */
@EnumOption(name = "filterDateFunction")
public enum FilterDateFunction implements IEnumOption<String>, FilterFunction {
    /**
     *
     */
    EQ("精确匹配", " = '%s' "),
    /**
     *
     */
    START_WITH("开始于", " >= '%s' "),
    /**
     *
     */
    END_WITH("结束于", " <= '%s' "),
    ;

    String title;
    String operation;
    FilterDateFunction(String title, String operation) {
        this.title = title;
        this.operation = operation;
    }

    /**
     * 枚举项 “键”
     *
     * @return key 键
     */
    @Override
    public String key() {
        return name();
    }

    /**
     * 枚举项 “值”
     *
     * @return value 值
     */
    @Override
    public String value() {
        return title;
    }

    /**
     * 枚举options
     *
     * @return 枚举项组成的map
     */
    @Override
    public Map<String, String> enums() {
        return Arrays.stream(values()).collect(Collectors.toMap(IEnumOption::key, IEnumOption::value));
    }

    public static FilterDateFunction parse(String name) {
        Optional<String> optional = Arrays.stream(values()).map(FilterDateFunction::name).filter(em -> em.equals(name)).findAny();
        return optional.isPresent() ? valueOf(name) : null;
    }

    /**
     * Applies this function to the given argument.
     *
     * @param s the function argument
     * @return the function result
     */
    @Override
    public String apply(String s) {
        return String.format(operation, s);
    }
}
